@*
    检验弹框页面
*@
@{
    Layout = null;
}
<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
<style>
    body {
        padding: 20px;
        background-color: #fff;
    }

    .layui-form-label {
        width: 100px;
    }

    .layui-input-block {
        margin-left: 130px;
    }

    .layui-form-item {
        margin-bottom: 15px;
    }

    .form-section {
        border: 1px solid #e6e6e6;
        padding: 15px;
        margin-bottom: 15px;
        border-radius: 2px;
    }

    .section-title {
        font-weight: bold;
        color: #333;
        margin-bottom: 15px;
        padding-bottom: 5px;
        border-bottom: 1px solid #e6e6e6;
    }

    .layui-textarea {
        min-height: 80px;
    }

    .readonly-input {
        background-color: #f2f2f2;
    }
</style>

<form class="layui-form" lay-filter="inspectionForm" id="inspectionForm">
    <input type="hidden" name="pendingTaskId" id="pendingTaskId" />
    
    <!-- 基本信息区域 -->
    <div class="form-section">
        <div class="section-title">基本信息</div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red;">*</span>检验单号</label>
                    <div class="layui-input-block">
                        <input type="text" name="inspectionCode" id="inspectionCodeInput" required lay-verify="required" placeholder="请输入检验单号" class="layui-input">
                        <div class="layui-form-mid layui-word-aux">
                            <input type="checkbox" name="autoGenerate" lay-skin="switch" lay-text="自动生成|手动输入" checked>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">检验单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="inspectionName" placeholder="请输入检验单名称" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red;">*</span>检测类型</label>
                    <div class="layui-input-block">
                        <select name="testingType" lay-verify="required">
                            <option value="">请选择检测类型</option>
                            <option value="过程检验">过程检验</option>
                            <option value="来料检验">来料检验</option>
                            <option value="成品检验">成品检验</option>
                            <option value="出货检验">出货检验</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red;">*</span>工作站号</label>
                    <div class="layui-input-block">
                        <input type="text" name="workstationCode" required lay-verify="required" placeholder="请输入工作站号" class="layui-input" value="WS0019">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 工单信息区域 -->
    <div class="form-section">
        <div class="section-title">工单信息</div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">工单编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="workOrderCode" placeholder="请输入工单编码" class="layui-input" value="MO20250617001">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">工单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="workOrderName" placeholder="请输入工单名称" class="layui-input" value="test617">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">工作站名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="workstationName" placeholder="请输入工作站名称" class="layui-input" value="ROXYNE">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red;">*</span>检测数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="testingQuantity" required lay-verify="required|number" placeholder="请输入检测数量" class="layui-input" value="1000.0">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 产品信息区域 -->
    <div class="form-section">
        <div class="section-title">产品信息</div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="productCode" placeholder="请输入产品编码" class="layui-input" value="CP00026">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="productName" placeholder="请输入产品名称" class="layui-input" value="测试">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">单位</label>
                    <div class="layui-input-block">
                        <input type="text" name="unit" placeholder="请输入单位" class="layui-input" value="g">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">规格型号</label>
                    <div class="layui-input-block">
                        <input type="text" name="specification" placeholder="请输入规格型号" class="layui-input" value="测试">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 任务信息区域 -->
    <div class="form-section">
        <div class="section-title">任务信息</div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">工序编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="processCode" placeholder="请输入工序编码" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">工序名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="processName" placeholder="请输入工序名称" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">任务编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="taskCode" placeholder="请输入任务编号" class="layui-input" value="TASK20250082">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red;">*</span>合格品数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="qualifiedQuantity" required lay-verify="required|number" placeholder="请输入合格品数量" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 检验详情区域 -->
    <div class="form-section">
        <div class="section-title">检验详情</div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">任务名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="taskName" placeholder="请输入任务名称" class="layui-input" value="TASK20250082">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">不合格数</label>
                    <div class="layui-input-block">
                        <input type="number" name="unqualifiedQuantity" placeholder="请输入不合格数" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">数据缺陷数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="defectQuantity" placeholder="请输入数据缺陷数量" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">产量缺陷数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="productionDefectQuantity" placeholder="请输入产量缺陷数量" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">轻微缺陷数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="minorDefectQuantity" placeholder="请输入轻微缺陷数量" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">检测日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="inspectionDate" id="inspectionDate" placeholder="请选择检测日期" class="layui-input" readonly>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">检测结果</label>
                    <div class="layui-input-block">
                        <select name="inspectionResult">
                            <option value="">请选择检测结果</option>
                            <option value="合格">合格</option>
                            <option value="不合格">不合格</option>
                            <option value="待复检">待复检</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">检测人员</label>
                    <div class="layui-input-block">
                        <input type="text" name="inspector" placeholder="请输入检测人员" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 备注区域 -->
    <div class="form-section">
        <div class="section-title">备注信息</div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
        <button class="layui-btn" lay-submit lay-filter="submitInspection" id="saveBtn">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" onclick="closeDialog()">取消</button>
    </div>
</form>

<script>
    layui.use(['form', 'layer', 'laydate'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.$;

        // 初始化日期控件
        laydate.render({
            elem: '#inspectionDate',
            type: 'datetime',
            value: new Date()
        });

        // 获取URL参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        // 页面初始化
        var pendingTaskId = getUrlParam('pendingTaskId');
        if (pendingTaskId) {
            $('#pendingTaskId').val(pendingTaskId);
            loadPendingTaskData(pendingTaskId);
        }

        // 自动生成检验单号
        function generateInspectionCode() {
            return 'IQ' + Date.now() + Math.floor(Math.random() * 900 + 100);
        }

        // 监听自动生成开关
        form.on('switch()', function(obj){
            if (obj.elem.name === 'autoGenerate') {
                if (obj.elem.checked) {
                    $('#inspectionCodeInput').val(generateInspectionCode()).prop('readonly', true);
                } else {
                    $('#inspectionCodeInput').val('').prop('readonly', false);
                }
            }
        });

        // 初始化时设置检验单号
        $('#inspectionCodeInput').val(generateInspectionCode()).prop('readonly', true);

        // 加载待检任务数据
        function loadPendingTaskData(taskId) {
            $.ajax({
                url: '/PendingTask/FanPendingTask?pendingTaskId=' + taskId,
                type: 'GET',
                success: function(data) {
                    if (data) {
                        // 填充表单数据
                        form.val('inspectionForm', {
                            testingType: data.testingItems,
                            productCode: data.materialCode,
                            productName: data.materialName,
                            testingQuantity: data.pendingNum
                        });
                    }
                }
            });
        }

        // 提交表单
        form.on('submit(submitInspection)', function(formData){
            var field = formData.field;
            var $btn = $('#saveBtn');
            $btn.addClass('layui-btn-disabled').attr('disabled', true).text('保存中...');

            $.ajax({
                url: '/PendingTask/SaveInspection',
                type: 'POST',
                data: field,
                success: function(res){
                    if(res > 0){
                        layer.msg('检验单保存成功', {
                            time: 2000,
                            end: function(){
                                closeDialog();
                            }
                        });
                    } else {
                        layer.msg('保存失败，请重试');
                    }
                },
                error: function(){
                    layer.msg('操作失败，请重试');
                },
                complete: function(){
                    $btn.removeClass('layui-btn-disabled').attr('disabled', false).text('保存');
                }
            });
            return false;
        });

        // 关闭弹框
        window.closeDialog = function() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        };
    });
</script> 